
	
	
	
	var session_id="";
	var session_key="";
	var op_count="";
	
	function GUcManagerHttpJsonp(url,dataJson){
		var Num="";for(var i=0;i<20;i++){Num+=Math.floor(Math.random()*10);}  
		var callback="jsonp"+new Date().getTime()+Num;
		var promise = new Promise(function (resolve, reject) {
			Ajax.init({				url:url,				type:"get",				dataType:"jsonp",				data:dataJson,				callback:callback,				time:"3000",				success:function(data){					resolve(data);				},				fail:function(ex){					 Num="";					  for(var i=0;i<20;i++)					  {					  Num+=Math.floor(Math.random()*10);					  }  					callback="callback"+new Date().getTime()+Num;					Ajax.init({						url:url,						type:"get",						dataType:"jsonp",						data:dataJson,						callback:callback,						time:"10000",						success:function(data){							resolve(data);						},						fail:function(ex){							resolve("fail");						}				   });				}		   });
		});
		return promise;
	}
	
	var final_password_error_count="3";//密码错误弹出验证码次数
	var source_app_id=getQueryString("source_app_id");
	var open_id=getQueryString("open_id");
	var source_plat=getQueryString("source_plat");
	var third_access_token=getQueryString("third_access_token");
	var callback_url=getQueryString("callback_url");
	var uccomponent_uc_coll="";
	var uc_captcha_t="";
	var isNeedCode=false;
	var smsCode="";
	/*获取会话*/
	try {
		getTenants().then(function(res) {
			try {
		    	if(isNotNull(app_third_account) && isNotNull($("#app_third_account")) && is_login_third_account==1){
		        	var app_third_accounts=app_third_account.split(",");
		        	for(var i=0;i<app_third_accounts.length;i++){
		        		if(app_third_account.indexOf(app_third_accounts[i])>-1){
		        			if(isNotNull(app_third_accounts[i])){
		        				if(isNotNull($("#"+app_third_accounts[i]))){
			        				$("#"+app_third_accounts[i]).show();
			        				$("#"+app_third_accounts[i]).attr("href","");
			        			}
		        			}
		        		}
		        	}
		        	for(var i=0;i<source_plat_item.length;i++){
						if(isNotNull(source_plat_item[i].source_plat)){
							if(isNotNull($("#"+source_plat_item[i].source_plat))){
								if(isNotNull($("#"+source_plat_item[i].source_plat))){
									if(isNotNull(callback_url)){
										ucComponent.setCookie(app_id+"_callback_url",callback_url,"0.05");
									}
									var this_login_redirect_url=source_plat_item[i].login_redirect_url;
									this_login_redirect_url=this_login_redirect_url.replace("/h5/","/passport/");
									$("#"+source_plat_item[i].source_plat).attr("href",this_login_redirect_url);
								}
							}
						}
					}
		        	$("#app_third_account").show();
		        	source_app_id=getQueryString("source_app_id");
		        	open_id=getQueryString("open_id");
		        	source_plat=getQueryString("source_plat");
		        	third_access_token=getQueryString("third_access_token");
		        	loginThirdAccount(source_plat,open_id,source_app_id,third_access_token);
		    }} catch (e) {}
		    
		    
		    
		    try {
		    	if(undefined==is_reg_tel || is_reg_tel==1 || undefined==is_reg_email || is_reg_email==1){
			    	$("#register_div_id").show();
			    }
		    	if(undefined==is_find_password || is_find_password==1){
		    		$("#is_find_password").show();
		    	}
			} catch (e) {
			}
		});
	} catch (e) {}
	document.onkeydown = function(e){ 
	    var ev = document.all ? window.event : e;
	    if(ev.keyCode==13) {
	    	$("#login-page-sub-btn").click(); //处理事件
	     }
	}
	
	function loginThirdAccount(source_plat,open_id,source_app_id,third_access_token){
		
		if(!isNotNull(org_name)){
			org_name="";
		}
		
		if(isNotNull(source_plat) && isNotNull(open_id) && isNotNull(source_app_id) && isNotNull(third_access_token)){
			$.ajax({
				 url: '../v0.9/ucsdk/proxy/users/third_tokens',
				 type: 'POST',
				 contentType: "application/json",
				 data:"{\"open_id\":\""+open_id+"\",\"org_name\":\""+org_name+"\",\"source_plat\":\""+source_plat+"\",\"app_id\":\""+source_app_id+"\",\"third_access_token\":\""+third_access_token+"\"}",
				 success: function(data) {
					 if(undefined!=data.user_id){
					     var user_id=data.user_id;
					     var mac_key=data.mac_key;
					     ucManager.thirdLoginSSO(data.access_token,data.refresh_token,mac_key,user_id).then(function(res){
					    	 callback_url=ucComponent.getCookie(app_id+"_callback_url");
					    	 if(isNotNull(callback_url)){
					    			top.location.href=callback_url;
					    	 }else{
				    	    	gotoLoginPage();
					    	 }
					     });
					 }else{
						 var errorMessage=$.i18n.prop("third_login-submit-error");
						 $($("#login-error-message-id").children()[0]).text(errorMessage);
						 $("#login-error-message-id").show();
						 getSession();
					 }
				 },error:function(data,status,e){
					 
				 }
			});
		}
	}
	
	/*页面国际化初始化*/
	document.getElementById("login-page-title").innerHTML=$.i18n.prop('login_page_title');
	document.title=$.i18n.prop('login_page_title');
	var bind_i18n_arr=$("[bind-i18n]");
	for(var i=0;i<bind_i18n_arr.length;i++){
		try {
			$(bind_i18n_arr[i]).text($.i18n.prop($(bind_i18n_arr[i]).attr("bind-i18n")));
			//try {$(bind_i18n_arr[i]).attr("placeholder",$.i18n.prop($(bind_i18n_arr[i]).attr("bind-i18n")));} catch (e) {}
		} catch (e) {
			// TODO: handle exception
		}
		try {
			$("#login_name").attr("placeholder",$.i18n.prop("login-account-placeholder-id"));
		} catch (e) {
		}
		try {
			$("#password").attr("placeholder",$.i18n.prop("login-password-placeholder-id"));
		} catch (e) {
		}
		try {
			$("#smscode").attr("placeholder",$.i18n.prop("login-piccode-placeholder-id"));
		} catch (e) {
		}
	}
	/**
	* 获取会话信息
	*/
	getSession();
	function getSession(){
		if(is_new_versions=="0"){
			getDeviceId().then(function(deviceId) {
				uccomponent_uc_coll=deviceId;
				$.ajax({
					 url: '../v0.9/ucsdk/proxy/session',
					 type: 'POST',
					 contentType: "application/json",
					 data:"{\"session_type\":\"1\",\"device_id\":\""+uccomponent_uc_coll+"\"}",
					 success: function(data) {
						session_id=data.session_id;
						session_key=data.session_key;
						op_count=data.op_count;
						if(op_count>=final_password_error_count){
					    	ucCaptcha.getCharacter(org_name).then(function(characterJson) {
								$("#login-pic-code").attr("src",characterJson.f);
								$("#login-piccode-div-id").show();
								uc_captcha_t=characterJson.t;
								isNeedCode=true;
							});
						}
					 }
				});
				
			});
		}else{
			ucManager.getSession(1).then(function(data) {
				if(ucManager.isOk(data)){
					session_id=data.session_id;
					session_key=data.session_key;
					is_normal=data.is_normal;
					if(!is_normal){
						ucCaptcha.getCharacter(app_id).then(function(characterJson) {
							$("#login-pic-code").attr("src",characterJson.f);
							$("#login-piccode-div-id").show();
							uc_captcha_t=characterJson.t;
							isNeedCode=true;
						});
					}
				}
			});
		}
	}
	
	/**
	 * 验证码图片点击事件
	 */
	$("#login-pic-code").click(function(){
		getSession();
	});
	/**
	 *登录操作
	 */ 
	
	$("#login-page-sub-btn").click(function(){
		var isSUB=true;
		var login_name=$("#login_name").val();
		var password=$("#password").val();
		if(login_name=="" || undefined==login_name || login_name.indexOf("请")>-1){
			$("#login-name-input").css("border","1px solid red");
			isSUB=false;
		}
		if(password=="" || undefined==password || password.indexOf("请")>-1){
			$("#password-input").css("border","1px solid red");
			isSUB=false;
		}
		smsCode=$("#smscode").val();
		
		try {
			if(op_count>=final_password_error_count){
				if(undefined==smsCode || ""==smsCode || smsCode.indexOf("请")>-1){
					$("#piccode-input").css("border","1px solid red");
					isSUB=false;
				}
			}else{
				smsCode="";
			}
		} catch (e) {
		}
		try {
			if(!is_normal){
				if(undefined==smsCode || ""==smsCode || smsCode.indexOf("请")>-1){
					$("#piccode-input").css("border","1px solid red");
					isSUB=false;
				}
			}else{
				smsCode="";
			}
		} catch (e) {
		}
		
		if(!isSUB){return};
		if(is_new_versions=="0"){
			if(isNeedCode){
				//客户端检验验证码
				 ucCaptcha.checkCharacter(org_name,uc_captcha_t,smsCode).then(function(checkCharacterJson) {
					 if(undefined!=checkCharacterJson.result && checkCharacterJson.result==true){
						 commonLogin();
					 }else{
						 $($("#login-error-message-id").children()[0]).text($.i18n.prop("login-submit-error-piccode"));
						 $("#login-error-message-id").show();
						 getSession();
						 return;
					 }
				 });
			}else{
				commonLogin();
			}
		}else{
			var login_name=$("#login_name").val();
			var password=$("#password").val();
			$("#login-loading-id").show();
			$("#login-page-sub-btn").text($.i18n.prop("login-page-sub-btning"));
			ucManager.login(login_name,password,session_id,session_key,smsCode).then(function(res) {
				if(ucManager.isOk(res)){
					 $("#login-loading-id").hide();
					 if(isNotNull(callback_url)){
			    			top.location.href=callback_url;
		    	     }else{
		    	    	gotoLoginPage();
		    	     }
				}else{
					$("#login-loading-id").hide();
					$("#login-page-sub-btn").text($.i18n.prop("login-page-sub-btn"));
					 var errorMessage=$.i18n.prop("login-submit-error-common");
					 if(res.message=="帐号不存在"){
						 errorMessage=$.i18n.prop("login-submit-error-account-not-exist");
					 }else if(res.message=="密码不正确"){
						 errorMessage=$.i18n.prop("login-submit-error-account-password");
					 }
					 else if(res.message=="无效的验证码"){
						 errorMessage=$.i18n.prop("login-submit-error-piccode");
					 }
					 $($("#login-error-message-id").children()[0]).text(errorMessage);
					 $("#login-error-message-id").show();
					getSession();
				}
			});
		}
	});
	
	function commonLogin(){
		 getUcColl().then(function(uccoll) {
			 var login_name=$("#login_name").val();
				var password=$("#password").val();
				login_name=ucComponent.ecryptContent(login_name, session_key);
				password=ucComponent.ecryptContent(getMD5Value(password), session_key);
				$("#login-loading-id").show();
				$("#login-page-sub-btn").text($.i18n.prop("login-page-sub-btning"));
				$.ajax({
					 url: '../v0.9/ucsdk/proxy/securitylogin',
					 type: 'POST',
					 contentType: "application/json",
					 data:"{\"uc_coll\":\""+uccoll+"\",\"tid\":\""+uc_captcha_t+"\",\"org_id\":\""+org_id+"\",\"org_name\":\""+org_name+"\",\"login_name\":\""+login_name+"\",\"is_vorg\":\""+is_vorg+"\",\"password\":\""+password+"\",\"session_id\":\""+session_id+"\",\"identify_code\":\""+smsCode+"\"}",
					 success: function(data) {
						 if(undefined!=data.user_id){
						     var user_id=data.user_id;
						     user_id=ucComponent.decryptByDES(user_id,session_key);
						     var mac_key=data.mac_key;
						     mac_key=ucComponent.decryptByDES(mac_key,session_key);
						     ucManager.thirdLoginSSO(data.access_token,data.refresh_token,mac_key,user_id).then(function(res){
						    	 if(isNotNull(callback_url)){
						    			top.location.href=callback_url;
						    	 }else{
					    	    	gotoLoginPage();
						    	 }
						     });
						 }else{
							 $("#login-loading-id").hide();
								$("#login-page-sub-btn").text($.i18n.prop("login-page-sub-btn"));
							 var errorMessage=$.i18n.prop("login-submit-error-common");
							 if(data.message=="帐号不存在"){
								 errorMessage=$.i18n.prop("login-submit-error-account-not-exist");
							 }else if(data.message=="密码不正确"){
								 errorMessage=$.i18n.prop("login-submit-error-account-password");
							 }
							 else if(data.message=="无效的验证码"){
								 errorMessage=$.i18n.prop("login-submit-error-piccode");
							 }
							 $($("#login-error-message-id").children()[0]).text(errorMessage);
							 $("#login-error-message-id").show();
							 getSession();
						 }
					 },error:function(){
						 $($("#login-error-message-id").children()[0]).text($.i18n.prop("login-submit-error-common"));
						 $("#login-error-message-id").show();
						 $("#login-loading-id").hide();
							$("#login-page-sub-btn").text($.i18n.prop("login-page-sub-btn"));
						 getSession();
					 }
				});
		 });
	}
	
	function getIndexLength(str,reg){
		var arr  = str.match(reg);
		try {
			return arr.length;
		} catch (e) {
			return 0;
		}
	}
	
	/**
	 * 登录状态自动跳转
	 */
	try {
		ucManager.loadComplete().then(function(response) {
			ucManager.isLogin().then(function(res) {
				if(res=="true"){
					if(isNotNull(callback_url)){
		    			top.location.href=callback_url;
		    	    }else{
		    	    	gotoLoginPage();
		    	    }
				}
		 	});
		});
	} catch (e) {
		ucManager.isLogin().then(function(res) {
			if(res=="true"){
				if(isNotNull(callback_url)){
	    			top.location.href=callback_url;
	    	    }else{
	    	    	gotoLoginPage();
	    	    }
			}
	 	});
	};









